﻿using UnityEngine;
using UnityEngine.UI;
using System;

namespace FrostweepGames.UniversalMicrophoneLibrary.Examples
{
    public class Example : MonoBehaviour
    {
        private AudioSource _audioSource;
        private AudioClip _bufferedAudioClip;


        private Button _startRecordButton,
                       _stopRecordButton,
                       _playRecordedAudioButton,
                       _initModuleButton;

        private Text _logText;

        private GameObject _canvas;

        private void Start()
        {
#if UNITY_WEBGL
            MicrophoneWebGL.RegisterRecordSuccessCallback(RecordSuccessEventHandler);
            MicrophoneWebGL.RegisterRecordFinishedCallback(RecordFinishedEventHandler);
            MicrophoneWebGL.RegisterRecordFailedCallback(RecordFailedEventHandler);
#endif

            _audioSource = GetComponent<AudioSource>();

            _canvas = GameObject.Find("Canvas");

            _startRecordButton = _canvas.transform.Find("Button_StartRecord").GetComponent<Button>();
            _stopRecordButton = _canvas.transform.Find("Button_StopRecord").GetComponent<Button>();
            _playRecordedAudioButton = _canvas.transform.Find("Button_PlayAudio").GetComponent<Button>();
            _initModuleButton = _canvas.transform.Find("Button_Init").GetComponent<Button>();

            _logText = _canvas.transform.Find("Text_Log").GetComponent<Text>();

            _startRecordButton.onClick.AddListener(StartRecord);
            _stopRecordButton.onClick.AddListener(StopRecord);
            _playRecordedAudioButton.onClick.AddListener(PlayRecordedAudio);
            _initModuleButton.onClick.AddListener(InitModule);
        }

        private void OnDestroy()
        {
//#if UNITY_WEBGL
//            MicrophoneWebGL.UnregisterRecordSuccessCallback(RecordSuccessEventHandler);
//            MicrophoneWebGL.UnregisterRecordFinishedCallback(RecordFinishedEventHandler);
//            MicrophoneWebGL.UnregisterRecordFailedCallback(RecordFailedEventHandler);
//#endif
        }

        private void RecordFailedEventHandler(string value)
        {
            _logText.text += "\nRecordFailedEventHandler !!!" + value;
        }

        private void RecordSuccessEventHandler()
        {
            _logText.text += "\nRecordSuccessEventHandler !!!";
        }

        private void RecordInfoEventHandler(string value)
        {
            _logText.text += Environment.NewLine + value;
        }

        private void RecordFinishedEventHandler()
        {
            _logText.text += "\nRecordFinishedEventHandler !!!";
        }

        private void InitModule()
        {
#if UNITY_WEBGL
            MicrophoneWebGL.Init();
#endif
        }

        private void StartRecord()
        {
#if UNITY_WEBGL
            MicrophoneWebGL.Start();
#endif
        }

        private void StopRecord()
        {
#if UNITY_WEBGL
            MicrophoneWebGL.End();
#endif
        }

        private void Reset()
        {
#if UNITY_WEBGL
            MicrophoneWebGL.Reset();
#endif
        }

        private void PlayRecordedAudio()
        {
#if UNITY_WEBGL
            if (MicrophoneWebGL.GetAudioClip() == null)
                return;

            _audioSource.clip = MicrophoneWebGL.GetAudioClip();
            _audioSource.Play();
#endif
        }
    }
}